{extend name="public/layout" /}
{block name="head"}{/block}
{block name="body"}
<body>
<div class="container-fluid p-t-15">
  <div class="row">
    <div class="col-sm-4 col-md-3 col-lg-2">
      <div class="card" id="left-menu" style="height:calc(100vh - 40px);overflow-y: auto;">
        <div class="card-header card-header-sm">
          <h4>内容模型</h4>
          <ul class="card-actions">
            <li>
              <button type="button" onclick="doAction.add();"><i class="mdi mdi-plus"></i></button>
            </li>
          </ul>
        </div>
        <table class="table table-hover table-no-bordered">
          <tr>
            <th>模型名称</th>
            <th style="width:60px;">操作</th>
          </tr>
          {volist name="table_list" id="vo"}
          <tr {eq name="$vo.table_name" value="$table_name" }class="info" {/eq}>
          <td>
            <a href="index.html?table_name={$vo.table_name}" class="text-info">{$vo.display_name}</a>
          </td>
          <td>
            <a href="javascript:void(0)" onclick="doAction.edit(this)" data-info='{:json_encode($vo)}'>
              <i class="mdi mdi-pencil text-default"></i>
            </a>
            <a href="javascript:void(0)" onclick="helper.doConfirm(this,'refresh')"
               data-url="del.html"
               data-params="id={$vo.id}"
               data-msg="<span class='text-danger'>删除模型将会删除对应分类及内容</span><br/>确认操作吗?">
              <i class="mdi mdi-archive text-default"></i>
            </a>
          </td>
          </tr>
          {/volist}
        </table>
      </div>
    </div>
    <div class="col-sm-8 col-md-9 col-lg-10">
      <div class="card">
        <div class="card-header card-header-sm">
          <h4>字段列表</h4>
          <div class="pull-right">
            <a href="javascript:void(0);" class="btn btn-sm btn-default" onclick="doAction.addField();"><i
                class="mdi mdi-plus"></i> 添加字段</a>
            <a href="javascript:void(0);" class="btn btn-sm btn-default" onclick="doAction.refresh();"><i
                class="mdi mdi-refresh"></i> 刷新缓存</a>
          </div>
        </div>
        <div class="card-body p-lr-15">
          <div class="table-responsive">
            <table class="table table-hover">
              <tr>
                <th style="width:50px;">Id</th>
                <th style="width:160px;">标签名</th>
                <th style="width:160px;">字段名</th>
                <th style="width:160px;">默认值</th>
                <th style="width:160px;">表单类型</th>
                <th style="width:80px;">位置</th>
                <th style="width:220px;">验证</th>
                <th style="width:120px;">表单状态</th>
                <th style="width:50px;">排序</th>
                <th style="width:160px;">操作</th>
              </tr>
              {volist name="field_list" id="vo"}
              <tr>
                <td>{$vo.id}</td>
                <td>
                  <a href="javascript:void(0)" class="f-editable"
                     data-type="text"
                     data-name="label_name"
                     data-pk="{$vo.id}"
                     data-url="/system/cms.field/editable.html?valid=require">{$vo.label_name}</a>
                </td>
                <td>{$vo.field_name}</td>
                <td>
                  <a href="javascript:void(0)" class="f-editable"
                     data-type="text"
                     data-name="default_value"
                     data-pk="{$vo.id}"
                     data-url="/system/cms.field/editable.html">{$vo.default_value}</a>
                </td>
                <td>{$vo.obj_type}</td>
                <td>{$vo.obj_location==1 ? '左' : '右'}</td>
                <td>
                  <a href="javascript:void(0)" class="f-editable"
                     data-type="text"
                     data-name="obj_valid"
                     data-pk="{$vo.id}"
                     data-url="/system/cms.field/editable.html">{$vo.obj_valid}</a>
                </td>
                <td>
                  {:\\helper\\Form::_switch('obj_status',$vo.obj_status,[
                  "data-id"=>$vo['id']
                  ])}
                </td>
                <td>
                  <a href="javascript:void(0)" class="f-editable"
                     data-type="text"
                     data-name="show_order"
                     data-pk="{$vo.id}"
                     data-url="/system/cms.field/editable.html?valid=require">{$vo.show_order}</a>
                </td>
                <td>
                  <a href="javascript:void(0)" onclick="doAction.editField('{$vo.id}')"
                     class="btn-xs btn btn-primary">
                    <i class="mdi mdi-pencil"></i> 编辑</a>
                  <a href="javascript:void(0)" class="btn-xs btn btn-danger"
                     onclick="helper.doConfirm(this,'refresh')"
                     data-url="/system/cms.field/del.html"
                     data-params="id={$vo.id}"
                     data-msg="确认要删除该权限规则吗?"><i class="mdi mdi-archive"></i> 删除</a>
                </td>
              </tr>
              {/volist}
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
{include file="cms/table/save" /}
<script>
  Do('editable', function () {
    $('.f-editable').editable();
  });
  $("input[name='obj_status']").change(function () {
    var id = $(this).data('id');
    var val = $(this).prop("checked") ? 1 : 0;
    $.post("/system/cms.field/multi.html", {id: id, name: "obj_status", value: val}, function (res) {
      if (res.code == 0) helper.tips(res.msg);
    })
  });
  var doAction = {
    add: function () {
      var info = {"id": "", "display_name": "", "table_name": "", "show_order": 1, "action": "add.html"};
      var formHtml = juicer($('#table-form-tpl').html(), {info: info});
      layer.open({
        type: 1,
        title: "添加模型",
        area: ['600px', '320px'],
        content: formHtml,
        success: function (layero, index) {
          formHelper.init();
          formHelper.subForm(function () {
            helper.refresh();
          });
        }
      });
    },
    edit: function (obj) {
      var info = $(obj).data('info');
      info.action = "edit.html";
      var formHtml = juicer($('#table-form-tpl').html(), {info: info});
      layer.open({
        type: 1,
        title: "修改模型",
        area: ['600px', '320px'],
        content: formHtml,
        success: function (layero, index) {
          formHelper.init();
          formHelper.subForm(function () {
            helper.refresh();
          });
        }
      });
    },
    addField: function () {
      var table = '{$table_name}';
      helper.createTab({
        title: '添加字段',
        url: '/system/cms.field/add.html?table_name=' + table
      });
    },
    editField: function (id) {
      helper.createTab({
        title: '修改字段',
        url: '/system/cms.field/edit.html?id=' + id
      });
    },
    refresh: function () {
      var html = juicer($('#refresh-tpl').html(), {});
      layer.open({
        type: 1,
        title: "刷新模型缓存",
        area: ['200px', '260px'],
        content: html,
        btn: ['刷新缓存'],
        success: function (layero, index) {
          formHelper.init();
          formHelper.subForm(function () {
            helper.tips('刷新成功');
          });
        },
        yes: function (index, layero) {
          var tableNames = helper.getCheckboxValue('#refresh-box');
          if (tableNames.length == 0) {
            helper.tips('请选择模型');
            return false;
          }
          helper.getAjax({
            url: 'refresh.html',
            data: {table_names: tableNames},
            success: function (res) {
              helper.tips(res.msg);
            }
          });
          layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
      });
    }
  };
</script>
</body>
{/block}